"""
旅游平台数据库模型
"""
from sqlalchemy import Column, Integer, String, DateTime, Boolean, Enum, ForeignKey, Float, Text
from sqlalchemy.orm import relationship
from datetime import datetime
import enum

# 使用现有的Base
from Models.login.models import Base


class AttractionType(str, enum.Enum):
    """景点类型"""
    SCENIC = "scenic"  # 景区
    HOTEL = "hotel"  # 酒店
    RESTAURANT = "restaurant"  # 餐厅
    ENTERTAINMENT = "entertainment"  # 娱乐
    SHOPPING = "shopping"  # 购物


class Attraction(Base):
    """景点表"""
    __tablename__ = "attractions"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(200), nullable=False, comment="景点名称")
    description = Column(Text, nullable=True, comment="景点描述")
    type = Column(String(50), nullable=False, default=AttractionType.SCENIC.value, comment="景点类型")
    
    # 位置信息
    province = Column(String(50), nullable=True, comment="省份")
    city = Column(String(50), nullable=True, comment="城市")
    district = Column(String(50), nullable=True, comment="区县")
    address = Column(String(500), nullable=True, comment="详细地址")
    longitude = Column(Float, nullable=True, comment="经度")
    latitude = Column(Float, nullable=True, comment="纬度")
    
    # 价格信息
    price = Column(Float, default=0.0, comment="价格")
    original_price = Column(Float, nullable=True, comment="原价")
    
    # 图片
    cover_image = Column(String(500), nullable=True, comment="封面图片")
    images = Column(Text, nullable=True, comment="图片列表（JSON格式）")
    
    # 评分和评价
    rating = Column(Float, default=0.0, comment="评分（0-5）")
    review_count = Column(Integer, default=0, comment="评价数量")
    
    # 特色标签
    tags = Column(String(500), nullable=True, comment="标签（逗号分隔）")
    
    # 商家信息
    merchant_id = Column(Integer, ForeignKey("users.id"), nullable=True, index=True, comment="商家用户ID")
    
    # 状态
    is_active = Column(Boolean, default=True, comment="是否启用")
    is_recommended = Column(Boolean, default=False, comment="是否推荐")
    
    # 时间字段
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    # 关系映射
    orders = relationship("Order", back_populates="attraction")
    favorites = relationship("Favorite", back_populates="attraction")
    reviews = relationship("Review", back_populates="attraction")
    
    def __repr__(self):
        return f"<Attraction(id={self.id}, name={self.name})>"


class Order(Base):
    """订单表"""
    __tablename__ = "orders"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    order_no = Column(String(50), unique=True, nullable=False, index=True, comment="订单号")
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True, comment="用户ID")
    attraction_id = Column(Integer, ForeignKey("attractions.id"), nullable=False, index=True, comment="景点ID")
    
    # 订单信息
    quantity = Column(Integer, default=1, comment="数量")
    unit_price = Column(Float, nullable=False, comment="单价")
    total_amount = Column(Float, nullable=False, comment="总金额")
    
    # 订单状态
    status = Column(String(20), default="pending", comment="订单状态：pending, paid, completed, cancelled")
    
    # 联系人信息
    contact_name = Column(String(100), nullable=True, comment="联系人姓名")
    contact_phone = Column(String(20), nullable=True, comment="联系人电话")
    
    # 备注
    notes = Column(Text, nullable=True, comment="备注")
    
    # 时间字段
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    paid_at = Column(DateTime, nullable=True, comment="支付时间")
    
    # 关系映射
    user = relationship("User")
    attraction = relationship("Attraction", back_populates="orders")
    payments = relationship("Payment", back_populates="order")
    refunds = relationship("Refund", back_populates="order")
    
    def __repr__(self):
        return f"<Order(id={self.id}, order_no={self.order_no})>"


class Favorite(Base):
    """收藏表"""
    __tablename__ = "favorites"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True, comment="用户ID")
    attraction_id = Column(Integer, ForeignKey("attractions.id"), nullable=False, index=True, comment="景点ID")
    
    created_at = Column(DateTime, default=datetime.now, comment="收藏时间")
    
    # 关系映射
    user = relationship("User")
    attraction = relationship("Attraction", back_populates="favorites")
    
    def __repr__(self):
        return f"<Favorite(id={self.id}, user_id={self.user_id}, attraction_id={self.attraction_id})>"


class Review(Base):
    """评价表"""
    __tablename__ = "reviews"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True, comment="用户ID")
    attraction_id = Column(Integer, ForeignKey("attractions.id"), nullable=False, index=True, comment="景点ID")
    order_id = Column(Integer, ForeignKey("orders.id"), nullable=True, comment="订单ID")
    
    # 评价内容
    rating = Column(Float, nullable=False, comment="评分（0-5）")
    content = Column(Text, nullable=True, comment="评价内容")
    images = Column(Text, nullable=True, comment="评价图片（JSON格式）")
    
    # 状态
    is_visible = Column(Boolean, default=True, comment="是否可见")
    
    # 时间字段
    created_at = Column(DateTime, default=datetime.now, comment="评价时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    # 关系映射
    user = relationship("User")
    attraction = relationship("Attraction", back_populates="reviews")
    
    def __repr__(self):
        return f"<Review(id={self.id}, user_id={self.user_id}, attraction_id={self.attraction_id})>"


class UserBehavior(Base):
    """用户行为表"""
    __tablename__ = "user_behaviors"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True, comment="用户ID")
    attraction_id = Column(Integer, ForeignKey("attractions.id"), nullable=False, index=True, comment="景点ID")
    
    behavior_type = Column(String(20), nullable=False, default="view", comment="行为类型：view/click/favorite/order/review")
    score = Column(Float, nullable=False, default=0.0, comment="行为权重/评分（0-1）")
    
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    
    user = relationship("User")
    attraction = relationship("Attraction")
    
    def __repr__(self):
        return f"<UserBehavior(id={self.id}, user_id={self.user_id}, attraction_id={self.attraction_id}, type={self.behavior_type})>"


class UserSegment(Base):
    """用户分层表"""
    __tablename__ = "user_segments"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, unique=True, index=True, comment="用户ID")
    segment = Column(String(20), nullable=False, default="normal", comment="分层：vip/active/normal/inactive")
    
    total_behaviors = Column(Integer, default=0, comment="行为总数")
    total_orders = Column(Integer, default=0, comment="订单总数")
    total_spent = Column(Float, default=0.0, comment="总消费金额")
    avg_rating = Column(Float, default=0.0, comment="平均评分")
    
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    user = relationship("User")
    
    def __repr__(self):
        return f"<UserSegment(user_id={self.user_id}, segment={self.segment})>"


class Banner(Base):
    """轮播图表"""
    __tablename__ = "banners"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    title = Column(String(200), nullable=True, comment="标题")
    image_url = Column(String(500), nullable=False, comment="图片URL")
    link_url = Column(String(500), nullable=True, comment="链接URL")
    link_type = Column(String(20), nullable=True, comment="链接类型：attraction, url")
    link_id = Column(Integer, nullable=True, comment="关联ID（如景点ID）")
    
    # 排序和状态
    sort = Column(Integer, default=0, comment="排序")
    is_active = Column(Boolean, default=True, comment="是否启用")
    
    # 时间字段
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    def __repr__(self):
        return f"<Banner(id={self.id}, title={self.title})>"


class Category(Base):
    """分类表"""
    __tablename__ = "categories"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(100), nullable=False, comment="分类名称")
    icon = Column(String(100), nullable=True, comment="图标")
    image_url = Column(String(500), nullable=True, comment="图片URL")
    
    # 排序和状态
    sort = Column(Integer, default=0, comment="排序")
    is_active = Column(Boolean, default=True, comment="是否启用")
    
    # 时间字段
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    def __repr__(self):
        return f"<Category(id={self.id}, name={self.name})>"

